package com.coolzeng.controller;

import com.coolzeng.entity.User;
import com.coolzeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;

/**
 * Created by Administrator on 2017/6/7.
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    //登录

    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
        public String login(@RequestParam("id") int id, @RequestParam("password") String password,HttpServletRequest request,HttpServletResponse response){


        User user = userService.Login(id);
        if (user.getPassword().equals(password)){
            HttpSession session=request.getSession();
            session.setAttribute("loginUser",user);
            String userid = String.valueOf(id);

            //获取客户端的cookie
            //request.getCookies();
            Cookie cookie=new Cookie("islogin","yes");
            Cookie cookie1=new Cookie("username",user.getUserName());
            Cookie cookie2=new Cookie("id",userid);
            cookie.setMaxAge(60*5);
            response.addCookie(cookie);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            return "islogin";
        }else {
            return "islogin";
        }

    }


}
